<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc,fixuphtml" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>Java Object Serialization Specification: 4 - Class Descriptors</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <link rel="stylesheet" href="../../resources/jdk-default.css" />
  <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
  <![endif]-->
</head>
<body>
<header id="title-block-header">
<h1 class="title">Java Object Serialization Specification: 4 - Class Descriptors</h1>
</header>
<main><ul>
<li><a href="#the-objectstreamclass-class">The ObjectStreamClass Class</a></li>
<li><a href="#dynamic-proxy-class-descriptors">Dynamic Proxy Class Descriptors</a></li>
<li><a href="#serialized-form">Serialized Form</a></li>
<li><a href="#the-objectstreamfield-class">The ObjectStreamField Class</a></li>
<li><a href="#inspecting-serializable-classes">Inspecting Serializable Classes</a></li>
<li><a href="#stream-unique-identifiers">Stream Unique Identifiers</a></li>
</ul>
<hr />
<h2 id="the-objectstreamclass-class">4.1 The ObjectStreamClass Class</h2>
<p>The <code>ObjectStreamClass</code> provides information about classes that are saved in a Serialization stream. The descriptor provides the fully-qualified name of the class and its serialization version UID. A <code>SerialVersionUID</code> identifies the unique original class version for which this class is capable of writing streams and from which it can read.</p>
<pre><code>package java.io;

public class ObjectStreamClass implements Serializable
{
    public static ObjectStreamClass lookup(Class&lt;?&gt; cl);

    public static ObjectStreamClass lookupAny(Class&lt;?&gt; cl);

    public String getName();

    public Class&lt;?&gt; forClass();

    public ObjectStreamField[] getFields();
    
    public ObjectStreamField getField(String name);

    public long getSerialVersionUID();

    public String toString();
}</code></pre>
<p>The <code>lookup</code> method returns the <code>ObjectStreamClass</code> descriptor for the specified class in the virtual machine. If the class has defined <code>serialVersionUID</code> it is retrieved from the class. If the <code>serialVersionUID</code> is not defined by the class, it is computed from the definition of the class in the virtual machine. If the specified class is not serializable or externalizable, <em>null</em> is returned.</p>
<p>The <code>lookupAny</code> method behaves like the <code>lookup</code> method, except that it returns the descriptor for any class, regardless of whether it implements <code>Serializable</code>. The <code>serialVersionUID</code> of a class that does not implement <code>Serializable</code> is <em>0L.</em></p>
<p>The <code>getName</code> method returns the name of the class, in the same format that is used by the <code>Class.getName</code> method.</p>
<p>The <code>forClass</code> method returns the <code>Class</code> in the local virtual machine if one was found by <code>ObjectInputStream.resolveClass</code> method. Otherwise, it returns <em>null</em>.</p>
<p>The <code>getFields</code> method returns an array of <code>ObjectStreamField</code> objects that represent the serializable fields of this class.</p>
<p>The <code>getSerialVersionUID</code> method returns the <code>serialVersionUID</code> of this class. Refer to <a href="#stream-unique-identifiers">Section 4.6, &quot;Stream Unique Identifiers&quot;</a>. If not specified by the class, the value returned is a hash computed from the class's name, interfaces, methods, and fields using the Secure Hash Algorithm (SHA) as defined by the National Institute of Standards.</p>
<p>The <code>toString</code> method returns a printable representation of the class descriptor including the name of the class and the <code>serialVersionUID</code>.</p>
<h2 id="dynamic-proxy-class-descriptors">4.2 Dynamic Proxy Class Descriptors</h2>
<p>ObjectStreamClass descriptors are also used to provide information about dynamic proxy classes (e.g., classes obtained via calls to the getProxyClass method of java.lang.reflect.Proxy) saved in a serialization stream. A dynamic proxy class itself has no serializable fields and a serialVersionUID of 0L. In other words, when the Class object for a dynamic proxy class is passed to the static lookup method of ObjectStreamClass, the returned ObjectStreamClass instance will have the following properties:</p>
<ul>
<li>Invoking its getSerialVersionUID method will return 0L.</li>
<li>Invoking its getFields method will return an array of length zero.</li>
<li>Invoking its getField method with any String argument will return null.</li>
</ul>
<h2 id="serialized-form">4.3 Serialized Form</h2>
<p>The serialized form of an ObjectStreamClass instance depends on whether or not the Class object it represents is serializable, externalizable, or a dynamic proxy class.</p>
<p>When an <code>ObjectStreamClass</code> instance that does not represent a dynamic proxy class is written to the stream, it writes the class name and <code>serialVersionUID</code>, flags, and the number of fields. Depending on the class, additional information may be written:</p>
<ul>
<li><p>For non-serializable classes, the number of fields is always zero. Neither the <code>SC_SERIALIZABLE</code> nor the <code>SC_EXTERNALIZABLE</code> flag bits are set.</p></li>
<li><p>For serializable classes, the <code>SC_SERIALIZABLE</code> flag is set, the number of fields counts the number of serializable fields and is followed by a descriptor for each serializable field. The descriptors are written in canonical order. The descriptors for primitive typed fields are written first sorted by field name followed by descriptors for the object typed fields sorted by field name. The names are sorted using <code>String.compareTo</code>. For details of the format, refer to <a href="protocol.html#grammar-for-the-stream-format">Section 6.4, &quot;Grammar for the Stream Format&quot;</a>.</p></li>
<li><p>For externalizable classes, flags includes the <code>SC_EXTERNALIZABLE</code> flag, and the number of fields is always zero.</p></li>
<li><p>For enum types, flags includes the <code>SC_ENUM</code> flag, and the number of fields is always zero.</p></li>
</ul>
<p>When an ObjectOutputStream serializes the ObjectStreamClass descriptor for a dynamic proxy class, as determined by passing its Class object to the isProxyClass method of java.lang.reflect.Proxy, it writes the number of interfaces that the dynamic proxy class implements, followed by the interface names. Interfaces are listed in the order that they are returned by invoking the getInterfaces method on the Class object of the dynamic proxy class.</p>
<p>The serialized representations of ObjectStreamClass descriptors for dynamic proxy classes and non-dynamic proxy classes are differentiated through the use of different typecodes (<code>TC_PROXYCLASSDESC</code> and <code>TC_CLASSDESC</code>, respectively); for a more detailed specification of the grammar, see <a href="protocol.html#grammar-for-the-stream-format">Section 6.4, &quot;Grammar for the Stream Format&quot;</a>.</p>
<h2 id="the-objectstreamfield-class">4.4 The ObjectStreamField Class</h2>
<p>An <code>ObjectStreamField</code> represents a serializable field of a serializable class. The serializable fields of a class can be retrieved from the <code>ObjectStreamClass</code>.</p>
<p>The special static serializable field, <code>serialPersistentFields</code>, is an array of <code>ObjectStreamField</code> components that is used to override the default serializable fields.</p>
<pre><code>package java.io;

public class ObjectStreamField implements Comparable&lt;Object&gt; {

    public ObjectStreamField(String fieldName,
                             Class&lt;?&gt; fieldType);

    public ObjectStreamField(String fieldName,
                             Class&lt;?&gt; fieldType,
                             boolean unshared);

    public String getName();

    public Class&lt;?&gt; getType();

    public String getTypeString();

    public char getTypeCode();

    public boolean isPrimitive();

    public boolean isUnshared();

    public int getOffset();

    protected void setOffset(int offset);

    public int compareTo(Object obj);

    public String toString();
}</code></pre>
<p><code>ObjectStreamField</code> objects are used to specify the serializable fields of a class or to describe the fields present in a stream. Its constructors accept arguments describing the field to represent: a string specifying the name of the field, a <code>Class</code> object specifying the type of the field, and a <code>boolean</code> flag (implicitly <code>false</code> for the two-argument constructor) indicating whether or not values of the represented field should be read and written as &quot;unshared&quot; objects if default serialization/deserialization is in use (see the descriptions of the <code>ObjectInputStream.readUnshared</code> and <code>ObjectOutputStream.writeUnshared</code> methods in <a href="input.html#the-objectinputstream-class">Section 3.1, &quot;The ObjectInputStream Class&quot;</a> and <a href="output.html#the-objectoutputstream-class">Section 2.1, &quot;The ObjectOutputStream Class&quot;</a>, respectively).</p>
<p>The <code>getName</code> method returns the name of the serializable field.</p>
<p>The <code>getType</code> method returns the type of the field.</p>
<p>The <code>getTypeString</code> method returns the type signature of the field.</p>
<p>The <code>getTypeCode</code> method returns a character encoding of the field type ('<code>B</code>' for <code>byte</code>, '<code>C</code>' for <code>char</code>, '<code>D</code>' for <code>double</code>, '<code>F</code>' for <code>float</code>, '<code>I</code>' for <code>int</code>, '<code>J</code>' for <code>long</code>, '<code>L</code>' for non-array object types, '<code>S</code>' for <code>short</code>, '<code>Z</code>' for <code>boolean</code>, and '<code>[</code>' for arrays).</p>
<p>The <code>isPrimitive</code> method returns <code>true</code> if the field is of primitive type, or <code>false</code> otherwise.</p>
<p>The <code>isUnshared</code> method returns <code>true</code> if values of the field should be written as &quot;unshared&quot; objects, or <code>false</code> otherwise.</p>
<p>The <code>getOffset</code> method returns the offset of the field's value within instance data of the class defining the field.</p>
<p>The <code>setOffset</code> method allows <code>ObjectStreamField</code> subclasses to modify the offset value returned by the <code>getOffset</code> method.</p>
<p>The <code>compareTo</code> method compares <code>ObjectStreamFields</code> for use in sorting. Primitive fields are ranked as &quot;smaller&quot; than non-primitive fields; fields otherwise equal are ranked alphabetically.</p>
<p>The <code>toString</code> method returns a printable representation with name and type.</p>
<h2 id="inspecting-serializable-classes">4.5 Inspecting Serializable Classes</h2>
<p>The program <em>serialver</em> can be used to find out if a class is serializable and to get its <code>serialVersionUID</code>.</p>
<p>When invoked on the command line with one or more class names, serialver prints the <code>serialVersionUID</code> for each class in a form suitable for copying into an evolving class. When invoked with no arguments, it prints a usage line.</p>
<h2 id="stream-unique-identifiers">4.6 Stream Unique Identifiers</h2>
<p>Each versioned class must identify the original class version for which it is capable of writing streams and from which it can read. For example, a versioned class must declare:</p>
<pre><code>private static final long serialVersionUID = 3487495895819393L;</code></pre>
<p>The stream-unique identifier is a 64-bit hash of the class name, interface class names, methods, and fields. The value must be declared in all versions of a class except the first. It may be declared in the original class but is not required. The value is fixed for all compatible classes. If the SUID is not declared for a class, the value defaults to the hash for that class. The <code>serialVersionUID</code> for dynamic proxy classes and enum types always have the value <em>0L</em>. Array classes cannot declare an explicit <code>serialVersionUID</code>, so they always have the default computed value, but the requirement for matching <code>serialVersionUID</code> values is waived for array classes. Record classes have a default <code>serialVersionUID</code> value of <code>0L</code>, but can declare an explicit <code>serialVersionUID</code>. The requirement for matching <code>serialVersionUID</code> values is waived for record classes.</p>
<p><strong>Note:</strong> It is strongly recommended that all serializable classes explicitly declare <code>serialVersionUID</code> values, since the default <code>serialVersionUID</code> computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected <code>serialVersionUID</code> conflicts during deserialization, causing deserialization to fail.</p>
<p>The initial version of an <code>Externalizable</code> class must output a stream data format that is extensible in the future. The initial version of the method <code>readExternal</code> has to be able to read the output format of all future versions of the method <code>writeExternal</code>.</p>
<p>The <code>serialVersionUID</code> is computed using the signature of a stream of bytes that reflect the class definition. The National Institute of Standards and Technology (NIST) Secure Hash Algorithm (SHA-1) is used to compute a signature for the stream. The first two 32-bit quantities are used to form a 64-bit hash. A <code>java.lang.DataOutputStream</code> is used to convert primitive data types to a sequence of bytes. The values input to the stream are defined by the Java Virtual Machine (VM) specification for classes. Class modifiers may include the <code>ACC_PUBLIC</code>, <code>ACC_FINAL</code>, <code>ACC_INTERFACE</code>, and <code>ACC_ABSTRACT</code> flags; other flags are ignored and do not affect <code>serialVersionUID</code> computation. Similarly, for field modifiers, only the <code>ACC_PUBLIC</code>, <code>ACC_PRIVATE</code>, <code>ACC_PROTECTED</code>, <code>ACC_STATIC</code>, <code>ACC_FINAL</code>, <code>ACC_VOLATILE</code>, and <code>ACC_TRANSIENT</code> flags are used when computing <code>serialVersionUID</code> values. For constructor and method modifiers, only the <code>ACC_PUBLIC</code>, <code>ACC_PRIVATE</code>, <code>ACC_PROTECTED</code>, <code>ACC_STATIC</code>, <code>ACC_FINAL</code>, <code>ACC_SYNCHRONIZED</code>, <code>ACC_NATIVE</code>, <code>ACC_ABSTRACT</code> and <code>ACC_STRICT</code> flags are used. Names and descriptors are written in the format used by the <code>java.io.DataOutputStream.writeUTF</code> method.</p>
<p>The sequence of items in the stream is as follows:</p>
<ol type="1">
<li><p>The class name.</p></li>
<li><p>The class modifiers written as a 32-bit integer.</p></li>
<li><p>The name of each interface sorted by name.</p></li>
<li><p>For each field of the class sorted by field name (except <code>private static</code> and <code>private transient</code> fields:</p>
<ol type="a">
<li><p>The name of the field.</p></li>
<li><p>The modifiers of the field written as a 32-bit integer.</p></li>
<li><p>The descriptor of the field.</p></li>
</ol></li>
<li><p>If a class initializer exists, write out the following:</p>
<ol type="a">
<li><p>The name of the method, <code>&lt;clinit&gt;</code>.</p></li>
<li><p>The modifier of the method, <code>java.lang.reflect.Modifier.STATIC</code>, written as a 32-bit integer.</p></li>
<li><p>The descriptor of the method, <code>()V</code>.</p></li>
</ol></li>
<li><p>For each non-<code>private</code> constructor sorted by method name and signature:</p>
<ol type="a">
<li><p>The name of the method, <code>&lt;init&gt;</code>.</p></li>
<li><p>The modifiers of the method written as a 32-bit integer.</p></li>
<li><p>The descriptor of the method.</p></li>
</ol></li>
<li><p>For each non-<code>private</code> method sorted by method name and signature:</p>
<ol type="a">
<li><p>The name of the method.</p></li>
<li><p>The modifiers of the method written as a 32-bit integer.</p></li>
<li><p>The descriptor of the method.</p></li>
</ol></li>
<li><p>The SHA-1 algorithm is executed on the stream of bytes produced by <code>DataOutputStream</code> and produces five 32-bit values <code>sha[0..4]</code>.</p></li>
<li><p>The hash value is assembled from the first and second 32-bit values of the SHA-1 message digest. If the result of the message digest, the five 32-bit words <code>H0 H1 H2 H3 H4</code>, is in an array of five <code>int</code> values named <code>sha</code>, the hash value would be computed as follows:</p></li>
</ol>
<pre><code>      long hash = ((sha[0] &gt;&gt;&gt; 24) &amp; 0xFF) |
                  ((sha[0] &gt;&gt;&gt; 16) &amp; 0xFF) &lt;&lt; 8 |
                  ((sha[0] &gt;&gt;&gt; 8) &amp; 0xFF) &lt;&lt; 16 |
                  ((sha[0] &gt;&gt;&gt; 0) &amp; 0xFF) &lt;&lt; 24 |
                  ((sha[1] &gt;&gt;&gt; 24) &amp; 0xFF) &lt;&lt; 32 |
                  ((sha[1] &gt;&gt;&gt; 16) &amp; 0xFF) &lt;&lt; 40 |
                  ((sha[1] &gt;&gt;&gt; 8) &amp; 0xFF) &lt;&lt; 48 |
                  ((sha[1] &gt;&gt;&gt; 0) &amp; 0xFF) &lt;&lt; 56;</code></pre>
</main><footer class="legal-footer"><hr/><a href="../../legal/copyright.html">Copyright</a> &copy; 1993, 2021, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.<br>All rights reserved. Use is subject to <a href="https://www.oracle.com/java/javase/terms/license/java17speclicense.html">license terms</a> and the <a href="https://www.oracle.com/technetwork/java/redist-137594.html">documentation redistribution policy</a>. <!-- Version 17.0.2+8-LTS-86 --></footer>
</body>
</html>